﻿using System;
using System.Net.NetworkInformation;

namespace Network_Demo
{
    public class ShowNetwork
    {
        public static void Main(string[] args)
        {

            #region 获取网卡相关信息
            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface adapter in nics)
            {
                Console.WriteLine("网络适配器标识符: " + adapter.Id);
                Console.WriteLine("描述:             " + adapter.Description);
                Console.WriteLine("网络适配器名称:   " + adapter.Name);
                Console.WriteLine("接口类型:         " + adapter.NetworkInterfaceType.ToString());
                Console.WriteLine("状态:             " + adapter.OperationalStatus.ToString());
                Console.WriteLine("网络接口速度:     " + adapter.Speed);
                Console.WriteLine("是否启用网络接口: " + adapter.SupportsMulticast);
                Console.WriteLine("是否支持IPv6协议: " + adapter.Supports(NetworkInterfaceComponent.IPv6));
                PhysicalAddress address = adapter.GetPhysicalAddress();
                Byte[] bytes = address.GetAddressBytes();
                Console.Write("MAC: ");
                for (int i = 0; i < bytes.Length; i++)
                {
                    Console.Write("{0}", bytes[i].ToString("X2"));
                    if (i != bytes.Length - 1)
                    {
                        Console.Write("-");
                    }
                }
                Console.WriteLine("\n==============分割线================");
                Console.WriteLine();
                Console.WriteLine();
            } 
            #endregion

            #region 获取IP地址

            Console.WriteLine("\n\n\n==============分割线================");
            Console.WriteLine("获取内网IP地址:");
            System.Net.IPHostEntry ips = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());
            foreach (System.Net.IPAddress ip in ips.AddressList)
            {
                Console.WriteLine(ip.ToString());
            }
            // Console.WriteLine("\n==============分割线================");
            //Console.WriteLine("获取外围IP地址:");
            //using (System.Net.WebClient wc = new WebClient())
            //{
            //    Console.WriteLine(wc.DownloadString("http://www.zu14.cn/ip/"));
            //} 
            #endregion

            Console.ReadKey();
        }
    }
}
